<template>
  <div class="login-container">
    <div class="login-frame">
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        autocomplete="on"
        label-position="left"
      >
        <!-- 语言切换 -->
        <div class="title-container">
          <h3 class="title">川大智购商城平台端管理系统</h3>
          <lang-select class="set-language" />
        </div>
        <div class="input-frame">
          <el-form-item prop="username">
            <span class="svg-container">
              <svg-icon icon-class="user" />
            </span>
            <el-input
              ref="username"
              v-model="loginForm.username"
              :placeholder="$t('login.username')"
              name="username"
              type="text"
              tabindex="1"
              autocomplete="on"
            />
          </el-form-item>

          <el-tooltip v-model="capsTooltip" content="大写锁定已打开" placement="right" manual>
            <el-form-item prop="password">
              <span class="svg-container">
                <svg-icon icon-class="password" />
              </span>
              <el-input
                :key="passwordType"
                ref="password"
                v-model="loginForm.password"
                :type="passwordType"
                :placeholder="$t('login.password')"
                name="password"
                tabindex="2"
                autocomplete="on"
                @keyup.native="checkCapslock"
                @blur="capsTooltip = false"
                @keyup.enter.native="handleLogin"
              />
              <span class="show-pwd" @click="showPwd">
                <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
              </span>
            </el-form-item>
          </el-tooltip>
        </div>
        <el-button
          :loading="loading"
          class="login-btn"
          @click.native.prevent="handleLogin"
        >{{ $t('login.logIn') }}</el-button>
      </el-form>
      <!-- 验证码 -->
      <Verify
        v-if="initVerify"
        ref="verify"
        :captcha-type="'blockPuzzle'"
        :img-size="{width:'400px',height:'200px'}"
        @success="login"
      />
    </div>
    <div class="circle">
      <ul>
        <li />
        <li />
        <li />
        <li />
        <li />
      </ul>
    </div>
    <div class="square">
      <ul>
        <li />
        <li />
        <li />
        <li />
        <li />
        <li />
      </ul>
    </div>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'
import LangSelect from '@/components/LangSelect'
import Verify from '@/components/Verifition/Verify'

export default {
  name: 'Login',
  components: { LangSelect, Verify },
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('请输入正确的用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不能少于6位'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: 'admin', // 用户名
        password: '123456', // 密码
        captcha: '' // 验证码
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      initVerify: false,
      redirect: undefined,
      otherQuery: {}
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    }
  },
  mounted() {
    if (this.loginForm.username === '') {
      this.$refs.username.focus()
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus()
    }
  },
  methods: {
    checkCapslock(e) {
      const { key } = e
      this.capsTooltip = key && key.length === 1 && (key >= 'A' && key <= 'Z')
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.initVerify = true
          this.$nextTick(() => {
            this.$refs.verify.show()
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    login(verifyResult) {
      console.log(verifyResult.captchaVerification)
      this.loading = true
      this.loginForm.captcha = verifyResult.captchaVerification
      this.$store.dispatch('user/login', this.loginForm)
        .then(() => {
          this.$router.push({ path: this.redirect || '/', query: this.otherQuery })
          this.loading = false
        })
        .catch((e) => {
          console.log(e)
          this.loading = false
        })
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    }
  }
}

</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;
  background: linear-gradient(200deg,#e3c5eb,#a9c1ed);
  background-size: cover;
  .login-frame {
    position: fixed;
    top: 0;
    bottom: 60px;
    left: 0;
    right: 0;
    display: flex;
    align-items: center;
    width: 600px;
    height: 500px;
    background: #c0c8fa;
    box-shadow: 0px 0px 80px 0px rgba(0, 0, 0, 0.1);
    margin: auto;
    .login-form {
      position: relative;
      width: 400px;
      max-width: 100%;
      padding: 160px 35px 0;
      margin: 0 auto;
      overflow: hidden;
    }

    .input-frame {
      position: relative;
      bottom: 90px;
    }

    .tips {
      font-size: 14px;
      color: #fff;
      margin-bottom: 10px;
      span {
        &:first-of-type {
          margin-right: 16px;
        }
      }
    }

    .svg-container {
      padding: 6px 5px 6px 15px;
      color: $dark_gray;
      vertical-align: middle;
      width: 30px;
      display: inline-block;
    }

    .title-container {
      position: relative;
      bottom: 100px;

      .title {
        font-size: 30px;
        color: $light_gray;
        margin: 0px auto 40px auto;
        text-align: center;
        font-weight: bold;
      }

      .set-language {
        color: #fff;
        position: absolute;
        top: 45px;
        font-size: 18px;
        right: 0px;
        cursor: pointer;
      }
    }

    .show-pwd {
      position: absolute;
      right: 10px;
      top: 7px;
      font-size: 16px;
      color: $dark_gray;
      cursor: pointer;
      user-select: none;
    }

    .login-btn {
      position: relative;
      width:100%;
      height: 40px;
      bottom: 70px;
      margin-top:0px;
      background: linear-gradient(-200deg,#fac0e7,#bbaaee);
      color: #fff;
      letter-spacing: 8px;
      cursor: pointer;
      transition:0.5s;
    }
    .login-btn:hover {
      background: linear-gradient(-200deg,#bbaaee,#fac0e7);
      background-position-x: -328px;
    }
  }
  ul li{
    /* 绝对定位 */
    position: absolute;
    border: 1px solid #fff;
    background-color: #fff;
    width: 30px;
    height: 30px;
    list-style: none;
    opacity: 0;
  }
  .square li{
    top: 40vh;
    left: 60vw;
    /* 执行动画：动画名 时长 线性的 无限次播放 */
    animation: square 10s linear infinite;
  }
  .square li:nth-child(2){
    top: 80vh;
    left: 10vw;
    /* 设置动画延迟时间 */
    animation-delay: 2s;
  }
  .square li:nth-child(3){
    top: 80vh;
    left: 85vw;
    animation-delay: 4s;
  }
  .square li:nth-child(4){
    top: 10vh;
    left: 70vw;
    animation-delay: 6s;
  }
  .square li:nth-child(5){
    top: 10vh;
    left: 10vw;
    animation-delay: 8s;
  }
  .square li:nth-child(6){
    top: 10vh;
    left: 40vw;
    animation-delay: 5s;
  }
  .circle li{
    bottom: 0;
    left: 15vm;
    /* 执行动画：动画名 时长 线性的 无限次播放 */
    animation: circle 10s linear infinite;
  }
  .circle li:nth-child(2){
    left: 20vw;
    /* 设置动画延迟时间 */
    animation-delay: 2s;
  }
  .circle li:nth-child(3){
      left: 55vw;
      animation-delay: 6s;
  }
  .circle li:nth-child(4){
      left: 70vw;
      animation-delay: 4s;
  }
  .circle li:nth-child(5){
      left: 90vw;
      animation-delay: 8s;
  }
  @keyframes square {
    0%{
        transform: scale(0) rotateY(0deg);
        opacity: 1;
    }
    100%{
        transform: scale(3) rotateY(1000deg);
        opacity: 0;
    }
  }
  @keyframes circle {
    0%{
        transform: scale(0) rotate(0deg);
        opacity: 1;
        bottom: 0;
        border-radius: 0;
    }
    100%{
        transform: scale(3) rotate(1000deg);
        opacity: 0;
        bottom: 90vh;
        border-radius: 50%;
    }
  }
}
</style>
